package com.example;

import com.example.config.AppConfig;
import com.example.task.TimerTask;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.concurrent.ExecutorService;

public class MainApp {
    public static void main(String[] args) {
        // 初始化Spring注解配置的IOC容器
        AnnotationConfigApplicationContext context = 
            new AnnotationConfigApplicationContext(AppConfig.class);
        
        System.out.println("Spring IOC容器初始化完成");
        
        // 从容器中获取线程池和任务对象
        ExecutorService executorService = context.getBean("taskExecutor", ExecutorService.class);
        TimerTask timerTask = context.getBean(TimerTask.class);
        
        // 提交任务到线程池执行
        executorService.submit(() -> timerTask.printTimeEverySecond());
        
        // 等待任务完成后关闭线程池和容器
        executorService.submit(() -> {
            try {
                // 等待1分钟确保任务完成
                Thread.sleep(61000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            executorService.shutdown();
            context.close();
            System.out.println("Spring IOC容器已关闭");
        });
    }
}
    